define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'yjqrcode/item/index' + location.search,
                    add_url: 'yjqrcode/item/add',
                    edit_url: 'yjqrcode/item/edit',
                    del_url: 'yjqrcode/item/del',
                    multi_url: 'yjqrcode/item/multi',
                    table: 'yjqrcode_item',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name')},
                        {field: 'image', title: __('Images'), events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'nums', title: __('Nums')},
                        {field: 'description', title: __('Description')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: "更新时间", operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', 
                        title: __('Operate'), 
                        table: table, 
                        events: Table.api.events.operate, 
                        buttons: [
                                {
                                    name: 'click',
                                    title: __('生成二维码'),
                                    classname: 'btn btn-xs btn-info btn-click',
                                    icon: 'fa fa-plus',
                                    click: function (index,value) {
                                        layer.prompt({title: '输入生成数量，并确认', formType: 0}, function(num, index){
                                            layer.close(index);
                                            $.ajax({
                                                url:'yjqrcode/item/makecode',
                                                type:'post',
                                                dataType:'json',
                                                data:{id:value.id,num:num},
                                                success:function(ret){
                                                    console.log(ret)
                                                    if(ret.code==1){
                                                        layer.msg(ret.msg)
                                                    }else{
                                                        Layer.alert(ret.msg);
                                                    }
                                                },
                                                error:function(){
                                                    layer.msg('操作失败,请重试')
                                                }
                                            })
                                        })
                                    }
                                },
                                {
                                    name: 'click',
                                    title: '导出产品二维码编码',
                                    classname: 'btn btn-xs btn-warning btn-click',
                                    icon: 'fa fa-file-excel-o',
                                    click: function (index,value) {
                                        window.open("item/export?id="+value.id)
                                    }
                                },
                            ],
                        formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'yjqrcode/item/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'yjqrcode/item/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'yjqrcode/item/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});